package com.github.catvod.spider;

import android.text.TextUtils;

import com.github.catvod.bean.Class;
import com.github.catvod.bean.Filter;
import com.github.catvod.bean.Result;
import com.github.catvod.bean.Vod;
import com.github.catvod.crawler.Spider;
import com.github.catvod.crawler.SpiderDebug;
import com.github.catvod.net.OkHttp;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

public class Gzyrdnt extends Spider {

    private final String url = "https://www.gzyrdnt.com";

    @Override
    public String homeContent(boolean filter) throws Exception {
        List<Class> classes = new ArrayList<>();
        List<Vod> list = new ArrayList<>();
        Document doc = Jsoup.parse(OkHttp.string(url));
        for (Element li : doc.select(".seach_node_top > ul.stui-header__menu").select("li")) {
            String typeName = li.select("a").text();
            String typeId = li.select("a").attr("href");
            classes.add(new Class(typeId, typeName));
        }
        for (Element div : doc.select("ul.stui-vodlist > li")) {
            String id = div.select("a").attr("href");
            String name = div.select(".title").text();
            String pic = div.select("a").attr("style");
            String tag = div.select(".text-bot-left").text();
//            String remark = div.select("div.bottom > div").get(0).text();
            Vod vod = new Vod(id, name, pic.replace("background-image: url('", "").replace("');", ""), tag);
            list.add(vod);
        }
        return Result.string(classes, list);
    }

    @Override
    public String categoryContent(String tid, String pg, boolean filter, HashMap<String, String> extend) throws Exception {
        List<Vod> list = new ArrayList<>();
        String target = pg.equals("1") ? url + tid : url + tid + "/0/0/0/0/" + pg;
        Document doc = Jsoup.parse(OkHttp.string(target));
        for (Element div : doc.select("ul.stui-vodlist > li")) {
            String id = div.select("a").attr("href");
            String name = div.select(".title").text();
            String pic = div.select("a").attr("style");
            String tag = div.select(".text-bot-left").text();
//            String remark = div.select("div.bottom > div").get(0).text();
            Vod vod = new Vod(id, name, pic.replace("background-image: url('", "").replace("');", ""), tag);
            list.add(vod);
        }
        return Result.string(list);
    }

    @Override
    public String detailContent(List<String> ids) throws Exception {
        String html = OkHttp.string(url + ids.get(0));
        Document doc = Jsoup.parse(html);
        String name = doc.select("h1.title").get(0).text().replace(" 在线观看完整版", "");
        Elements sourceList = doc.select("div.v1-div").select("a");
        List<String> vodItems = new ArrayList<>();
        for (Element a : sourceList) {
            String episodeUrl = a.attr("href");
            String episodeName = a.text();
            if (episodeName.contains("下载APP")) {
                continue;
            }
            vodItems.add(episodeName + "$" + url + episodeUrl);
        }
        Vod vod = new Vod();
//        vod.setTypeName();
        vod.setVodId(ids.get(0));
        vod.setVodDirector(doc.select("p.data").get(2).select("a").text());
        vod.setVodActor(doc.select("p.data").get(1).select("a").text());
//        vod.setVodPic("https://" + pic);
        vod.setVodName(name);
        vod.setVodPlayFrom("片库");
        vod.setVodContent(doc.select(".stui-content__detail").get(1).text().split("剧情简介：")[1]);
        vod.setVodPlayUrl(TextUtils.join("#", vodItems));
        vod.setVodArea(doc.select("p.data").get(0).select("a").text());
        String[] year = doc.select("p.data").get(0).text().split(" ");
        vod.setVodYear(year[year.length - 1]);
        vod.setVodRemarks(doc.select("p.data").get(3).text());
        return Result.string(vod);
    }

    @Override
    public String playerContent(String flag, String id, List<String> vipFlags) throws Exception {
        return Result.get().url(id).parse().click("document.getElementsByClassName('vjs-big-play-button')[0].click()").string();
    }

    @Override
    public boolean manualVideoCheck() throws Exception {
        return true;
    }

    @Override
    public boolean isVideoFormat(String url) throws Exception {
        return !url.contains("afcdn.net") && url.contains(".m3u8");
    }

    @Override
    public String searchContent(String key, boolean quick) throws Exception {
        return searchContent("/se-rsmyf/" + key + "-1");
    }

    @Override
    public String searchContent(String key, boolean quick, String pg) throws Exception {
        return searchContent("/se-rsmyf/" + key + "-" + pg);
    }

    private String searchContent(String query) {
        List<Vod> list = new ArrayList<>();
        Document doc = Jsoup.parse(OkHttp.string(url + query));
        for (Element div : doc.select("ul.stui-vodlist > li")) {
            String id = div.select("a").attr("href");
            String name = div.select(".title").text();
            String pic = div.select("a").attr("style");
            String tag = div.select(".text-bot-left").text();
//            String remark = div.select("div.bottom > div").get(0).text();
            Vod vod = new Vod(id, name, pic.replace("background-image: url('", "").replace("');", ""), tag);
            list.add(vod);
        }
        return Result.string(list);
    }
}

